package com.wtwd.campus.h5_manager.controller;

import com.wtwd.campus.h5_manager.common.Constant;
import com.wtwd.campus.h5_manager.common.RespCode;
import com.wtwd.campus.h5_manager.common.RespEntity;
import com.wtwd.campus.h5_manager.entity.H5ManageUser;
import com.wtwd.campus.h5_manager.entity.dto.LoginDto;
import com.wtwd.campus.h5_manager.entity.vo.LoginVo;
import com.wtwd.campus.h5_manager.entity.vo.RolePermissionVo;
import com.wtwd.campus.h5_manager.service.H5ManagerUserService;
import com.wtwd.campus.h5_manager.service.H5MangerSysRolePermissionService;
import com.wtwd.campus.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author mjy
 * @date 2022/03/03
 * @description
 */
@Api(tags = "登录")
@RestController
@RequestMapping("/webH5-manager")
public class LoginController {


    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);


    @Autowired
    private H5ManagerUserService h5ManagerUserService;

    @Autowired
    private H5MangerSysRolePermissionService rolePermissionService;

    @Autowired
    private RedisService redisService;


    @ApiOperation(value = "h5日志平台登录")
    @ApiResponses({@ApiResponse(code = 604, message = "参数不能为空")
            , @ApiResponse(code = 601, message = "用户名或者密码错误")
    })
    @PostMapping("/login")
    public RespEntity<LoginVo> Login(@RequestBody LoginDto loginDto) {
        logger.info("h5-manager Login param={}", loginDto.toString());
        if (loginDto == null || StringUtils.isBlank(loginDto.getAccount()) || StringUtils.isBlank(loginDto.getPassword())) {
            return new RespEntity<>(RespCode.NO_PARAM);
        }
        H5ManageUser sysUser = h5ManagerUserService.getSysUserByAccount(loginDto.getAccount());
        Boolean checkLoginUser = h5ManagerUserService.checkLoginUser(sysUser, loginDto.getPassword());
        if (!checkLoginUser) {
            return new RespEntity(RespCode.USER_NAME_OR_PASSWORD_ERROR);
        }
        String token = h5ManagerUserService.createUserToken(Constant.H5_MANAGE_USER_LOGIN_KEY_PREFIX + sysUser.getUserId(), sysUser.getUserId(), Constant.TOKEN_EXPIRE, TimeUnit.MINUTES);
        List<RolePermissionVo> rolePermissionVos = rolePermissionService.getRolePermissionList(sysUser.getRoleId());
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setRolePermissionVos(rolePermissionVos);
        return new RespEntity<>(RespCode.SUCCESS, loginVo);
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public RespEntity logout(HttpServletRequest request) {
        logger.info("logout" + request);
        String token = request.getHeader(Constant.H5_MANAGE_TOKEN_KEY);
        redisService.deleteKey(token);
        return new RespEntity(RespCode.SUCCESS);
    }

}

